package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.domain.SettleFeeSheet;
import com.ruoyi.cderp.domain.SettleFeeSheetDetail;
import com.ruoyi.cderp.mapper.SettleFeeSheetDetailMapper;
import com.ruoyi.cderp.mapper.SettlePreSheetDetailMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.SettlePreSheetDetail;
import com.ruoyi.cderp.mapper.SettlePreSheetMapper;
import com.ruoyi.cderp.domain.SettlePreSheet;
import com.ruoyi.cderp.service.ISettlePreSheetService;

/**
 * 供应商预付款单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class SettlePreSheetServiceImpl implements ISettlePreSheetService
{
    @Autowired
    private SettlePreSheetMapper settlePreSheetMapper;

    @Autowired
    private SettlePreSheetDetailMapper settlePreSheetDetailMapper;

    LoginUser loginUser = null;
    /**
     * 查询供应商预付款单
     *
     * @param id 供应商预付款单主键
     * @return 供应商预付款单
     */
    @Override
    public SettlePreSheet selectSettlePreSheetById(String id)
    {
        SettlePreSheet settlePreSheet= settlePreSheetMapper.selectSettlePreSheetById(id);
        List<SettlePreSheetDetail> list = settlePreSheetDetailMapper.selectSettlePreSheetDetailOut(id);
        settlePreSheet.setSettlePreSheetDetailList(list);
        return settlePreSheet;
    }

    /**
     * 查询供应商预付款单列表
     *
     * @param settlePreSheet 供应商预付款单
     * @return 供应商预付款单
     */
    @Override
    public List<SettlePreSheet> selectSettlePreSheetList(SettlePreSheet settlePreSheet)
    {
        return settlePreSheetMapper.selectSettlePreSheetList(settlePreSheet);
    }

    /**
     * 新增供应商预付款单
     *
     * @param settlePreSheet 供应商预付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSettlePreSheet(SettlePreSheet settlePreSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settlePreSheet.setCreateById(loginUser.getUser().getUserId().toString());
        settlePreSheet.setCreateBy(loginUser.getUser().getUserName());
        settlePreSheet.setCreateTime(DateUtils.getNowDate());
        int rows = settlePreSheetMapper.insertSettlePreSheet(settlePreSheet);
        insertSettlePreSheetDetail(settlePreSheet);
        return rows;
    }

    /**
     * 修改供应商预付款单
     *
     * @param settlePreSheet 供应商预付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSettlePreSheet(SettlePreSheet settlePreSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settlePreSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        settlePreSheet.setUpdateBy(loginUser.getUser().getUserName());
        settlePreSheet.setUpdateTime(DateUtils.getNowDate());
        settlePreSheetMapper.deleteSettlePreSheetDetailBySheetId(settlePreSheet.getId());
        insertSettlePreSheetDetail(settlePreSheet);
        return settlePreSheetMapper.updateSettlePreSheet(settlePreSheet);
    }

    /**
     * 批量删除供应商预付款单
     *
     * @param ids 需要删除的供应商预付款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettlePreSheetByIds(String[] ids)
    {
        settlePreSheetMapper.deleteSettlePreSheetDetailBySheetIds(ids);
        return settlePreSheetMapper.deleteSettlePreSheetByIds(ids);
    }

    /**
     * 删除供应商预付款单信息
     *
     * @param id 供应商预付款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettlePreSheetById(String id)
    {
        settlePreSheetMapper.deleteSettlePreSheetDetailBySheetId(id);
        return settlePreSheetMapper.deleteSettlePreSheetById(id);
    }

    /**
     * 新增供应商预付款单明细信息
     *
     * @param settlePreSheet 供应商预付款单对象
     */
    public void insertSettlePreSheetDetail(SettlePreSheet settlePreSheet)
    {
        List<SettlePreSheetDetail> settlePreSheetDetailList = settlePreSheet.getSettlePreSheetDetailList();
        String id = settlePreSheet.getId();
        if (StringUtils.isNotNull(settlePreSheetDetailList))
        {
            List<SettlePreSheetDetail> list = new ArrayList<SettlePreSheetDetail>();
            for (SettlePreSheetDetail settlePreSheetDetail : settlePreSheetDetailList)
            {
                settlePreSheetDetail.setSheetId(id);
                list.add(settlePreSheetDetail);
            }
            if (list.size() > 0)
            {
                settlePreSheetMapper.batchSettlePreSheetDetail(list);
            }
        }
    }
}
